/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import entity.Cliente;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Tiago
 */
public class ClienteDAO {

    private Connection connection;

    public ClienteDAO() {
        this.connection = new ConnectionFactory().getConnection();
    }

    public void insere(Cliente cliente) {
        String sql = "INSERT INTO cliente (nome, endereco, email, cpf, dt_nasc, sexo) VALUES (?,?,?,?,?,?)";
        try {
            PreparedStatement stmt = connection.prepareStatement(sql);
            stmt.setString(1, cliente.getNome());
            stmt.setString(2, cliente.getEndereco());
            stmt.setString(3, cliente.getEmail());
            stmt.setString(4, cliente.getCpf());
            stmt.setDate(5, cliente.getDt_nasc());
            stmt.setString(6, cliente.getSexo());
            stmt.executeUpdate();
            stmt.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public void altera(Cliente cliente) {
        String sql = "UPDATE cliente SET nome=?, email=?, endereco=?, dt_nasc=? WHERE idCliente = ?";
        try {
            PreparedStatement stmt = connection.prepareStatement(sql);
            stmt.setString(1, cliente.getNome());
            stmt.setString(2, cliente.getEmail());
            stmt.setString(3, cliente.getEndereco());
            stmt.setDate(4, cliente.getDt_nasc());
            stmt.setInt(5, cliente.getIdCliente());
            stmt.executeUpdate();
            stmt.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    
    public List<Cliente> listar() throws SQLException {
        String sql = "SELECT * FROM cliente";
        List<Cliente> cliente = new ArrayList();
        PreparedStatement stmt = connection.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery();
        while (rs.next()) {
            Cliente cli = new Cliente();
            cli.setNome(rs.getString("nome"));
            cli.setIdCliente(rs.getInt("idCliente"));
            cli.setEmail(rs.getString("email"));
            cli.setEndereco(rs.getString("endereco"));
            cli.setDt_nasc(rs.getDate("dt_nasc"));
            cli.setCpf(rs.getString("cpf"));
            cli.setSexo(rs.getString("sexo"));
            cliente.add(cli);
        }
        return cliente;
    }

    public Cliente buscaCli(int idCliente) throws SQLException{
        String sql = "SELECT * FROM cliente WHERE idCliente=?";
        PreparedStatement stmt = connection.prepareStatement(sql);
        stmt.setInt(1, idCliente);
        ResultSet rs = stmt.executeQuery();
        Cliente cliente = new Cliente();
        while(rs.next()){            
            cliente.setNome(rs.getString("nome"));
            cliente.setIdCliente(rs.getInt("idCliente"));  
            cliente.setEmail(rs.getString("email"));
            cliente.setEndereco(rs.getString("endereco"));
            cliente.setDt_nasc(rs.getDate("dt_nasc"));
            cliente.setCpf(rs.getString("cpf"));
            cliente.setSexo(rs.getString("sexo"));
        }
        return cliente;
        }
    
        public boolean cpfExistente(String cpf) throws SQLException{
        boolean resultado = false;
        String sql = "SELECT * FROM cliente WHERE cpf=?";
        PreparedStatement stmt = connection.prepareStatement(sql);
        stmt.setString(1, cpf);
        ResultSet rs = stmt.executeQuery();
        Cliente cliente = new Cliente();
        if(rs.next()){  
            resultado = true;  
        } 
        return resultado;
        }
    
     public void excluir(Cliente cliente) {
        String sql = "DELETE FROM cliente WHERE idCliente = ?";
        try {
            PreparedStatement stmt = connection.prepareStatement(sql);
            stmt.setInt(1, cliente.getIdCliente());
            stmt.executeUpdate();
            stmt.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

 public List<Cliente> procurar(String str) throws SQLException{
        String sql = "SELECT idCliente, nome,email,endereco, cpf, dt_nasc FROM cliente WHERE nome LIKE ? ";
        PreparedStatement stmt = connection.prepareStatement(sql);
        stmt.setString(1, "%"+str+"%");        
        List<Cliente> cliente = new ArrayList();
        ResultSet rs = stmt.executeQuery();        
        while (rs.next()) {
            Cliente cli = new Cliente();
            cli.setIdCliente(rs.getInt("idCliente")); 
            cli.setNome(rs.getString("nome"));
            cli.setEmail(rs.getString("email"));
            cli.setEndereco(rs.getString("endereco"));
            cli.setDt_nasc(rs.getDate("dt_nasc"));
            cli.setCpf(rs.getString("cpf"));
            cliente.add(cli);
        }
        stmt.close();
        return cliente;
    }
 
}
